﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using DataAccessObject;
using BUSINESS;
using System.Collections.ObjectModel;

namespace GUI.GiaoVien
{
    /// <summary>
    /// Interaction logic for GUIGiaoVien.xaml
    /// </summary>
    public partial class GUIGiaoVien : Window
    {
        private int _noOfErrors = 0;
        private GUIState state = GUIState.CAP_NHAT;

        #region
        public GUIGiaoVien()
        {
            InitializeComponent();
            InitializeControls();
        }

        private void InitializeControls()
        {
            txtNgayVaoLam.SelectedDate = DateTime.Now;
            txtNgaySinh.SelectedDate = new DateTime(1900, 1, 1);
            ////////
            InitListData();
            ///////
            ResetButton();
        }


        private void btnCapNhat_Click(object sender, RoutedEventArgs e)
        {
            update();
        }

        private void btnThemMoi_Click(object sender, RoutedEventArgs e)
        {
            ResetForm();
            state = GUIState.THEM_MOI;
            SaveState();
        }

        private void Validation_Error(object sender, ValidationErrorEventArgs e)
        {
            if (e.Action == ValidationErrorEventAction.Added)
                _noOfErrors++;
            else
                _noOfErrors--;
        }

        private void UpdateState()
        {
            btnCapNhat.IsEnabled = true;
            btnXoa.IsEnabled = true;
            btnLuuLai.IsEnabled = false;
        }

        private void SaveState()
        {
            btnXoa.IsEnabled = false;
            btnLuuLai.IsEnabled = true;
            btnCapNhat.IsEnabled = false;
        }


        private void Add_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = _noOfErrors == 0;
            e.Handled = true;
        }

        private void Add_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            save();
        }

        private void tbGiaoVien_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            state = GUIState.CAP_NHAT;
            UpdateState();
            UpdateData(sender);

        }

        private void setErrorMessage(string message)
        {
            errorAlert.Visibility = Visibility.Hidden;
            if (message == null || message == string.Empty)
            {
                ResetForm();
                SaveState();
                return;
            }
            txtError.Content = message;
            errorAlert.Visibility = Visibility.Visible;
        }

        private void ResetForm()
        {
            txtCMND.Text = txtDiaChi.Text = txtGioiTinh.Text = txtHoTen.Text = txtMaGiaoVien.Text = txtQueQuan.Text = string.Empty;
            errorAlert.Visibility = Visibility.Hidden;
        }

        private void ResetButton()
        {

        }

        private void btnXoa_Click(object sender, RoutedEventArgs e)
        {
            delete();
            ResetForm();
            SaveState();
        }

        #endregion

        #region editable
        private List<GIAOVIEN> giaoviens;
        private void update()
        {
            GIAOVIEN giaovien = tbGiaoVien.SelectedItem as GIAOVIEN;
            GIAOVIEN gv = mainGrid.DataContext as GIAOVIEN;
            giaovien.copyData(gv);
            string result = GiaoVienBUS.updateGiaoVien(giaovien);
            setErrorMessage(result);
        }

        private void save()
        {
            GIAOVIEN giaovien = new GIAOVIEN();
            giaovien.copyData((mainGrid.DataContext as GIAOVIEN));
            string result = GiaoVienBUS.InsertGiaoVien(giaovien);
            if (result == string.Empty)
            {
                giaoviens.Add(giaovien);
                tbGiaoVien.Items.Refresh();
            }

            setErrorMessage(result);
        }

        private void UpdateData(object sender)
        {
            GIAOVIEN giaovien = (sender as DataGrid).SelectedItem as GIAOVIEN;
            setData(giaovien);
        }

        private void setData(GIAOVIEN giaovien)
        {
            txtMaGiaoVien.Text = giaovien.MaGiaoVien;
            txtHoTen.Text = giaovien.HoTen;
            txtGioiTinh.SelectedIndex = giaovien.GioiTinh ? 0 : 1;
            txtDiaChi.Text = giaovien.DiaChi;
            txtNgaySinh.SelectedDate = giaovien.NgaySinh;
            txtNgayNghiViec.SelectedDate = giaovien.NgayNghiViec;
            txtNgayVaoLam.SelectedDate = giaovien.NgayVaoLam;
            txtQueQuan.Text = giaovien.QueQuan;
            txtCMND.Text = giaovien.CMND;
        }

        private void delete()
        {
            GIAOVIEN giaovien = tbGiaoVien.SelectedItem as GIAOVIEN;
            GiaoVienBUS.deleteGiaoVien(giaovien);
            giaoviens.Remove(giaovien);
            tbGiaoVien.Items.Refresh();
            if (giaoviens.Count() > 0)
            {
                tbGiaoVien.SelectedIndex = 0;
                return;
            }
        }

        private void InitListData()
        {
            giaoviens = GiaoVienBUS.getAllGiaoVien();
            tbGiaoVien.ItemsSource = giaoviens;
        }
        #endregion
    }
}
